package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/exitServlet")
public class exitServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//请求编码
        resp.setContentType("text/html;charset=utf-8");//响应编码
        // 获取所有的session ID
        Enumeration<String> sessionIds = req.getSession().getServletContext().getAttributeNames();

        // 遍历所有的session ID并删除
        while (sessionIds.hasMoreElements()) {
            String sessionId = sessionIds.nextElement();
            HttpSession session = req.getSession(false);
            if (session != null) {
                session.invalidate();
            }
        }
        // 清除完毕
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }
}
